#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.8.1
 Author:         myName

 Script Function:
	Template AutoIt script.

#ce ----------------------------------------------------------------------------

#include "ocr.au3"

Global $shop_reference_cnt = 0
Global $shop_reference[10000][4]

Global $shop_path_item = "item"
Global $shop_path_info = "info"
Global $shop_file_in = "shop-in.txt"
Global $shop_file_out = "shop-out.txt"
Global $shop_file_html = "html\all-items.html"
Global $need_shop_load = True


Func detect_shop_item(ByRef $colors, $create = True)
	If $need_shop_load Then
		import_shop_reference()
		$need_shop_load = False
	EndIf

	Local $found = -1
	For $i = 0 To $shop_reference_cnt-1
		Local $has = True
		For $j = 0 To 3
			If $shop_reference[$i][$j] <> $colors[$j] Then
				$has = False
				ExitLoop
			EndIf
		Next
		If $has Then
			Return $i
		EndIf
	Next
	
	If $create Then
		$found = $shop_reference_cnt
		For $j = 0 To 3
			$shop_reference[$found][$j] = $colors[$j]
		Next
		
		_DebugOut("new item: " & $found & " c: " _
			& "[" _
			& "0x" & Hex($colors[0],6) & "," _
			& "0x" & Hex($colors[1],6) & "," _
			& "0x" & Hex($colors[2],6) & "," _
			& "0x" & Hex($colors[3],6) & _
			"]")

		$shop_reference_cnt += 1
		SetExtended(1)
	EndIf
	Return $found
EndFunc

Func store_shop_item($pos,$x,$y,ByRef $ca,$num)
	Local $count = detect_shop_item($ca)
	Local $new = @extended
	
	Local $p = $winlist[$pos]
	Local _
		$l = $p[0] + $p[2] + $shop_box[0] + $x, _
		$t = $p[1] + $p[3] + $shop_box[1] + $y, _
		$r = $l + 58, _
		$b = $t + 79
	If $new Then
		Local $file = $shop_path_item & "\item-" & $count & ".jpg"
		_ScreenCapture_Capture($file,$l,$t,$r,$b)
		_DebugOut("store item " & $file)
		export_shop_reference()
		store_shop_item_info($pos,$num,$count)
	Else
		_DebugOut("found item " & $count)
	EndIf
	
	Return $count
EndFunc

Global $item_info_hBMP
Global $item_info_tBits
Global $item_info_width
Global $item_info_height
Global $item_info_length
Global $item_info_durty = True

Func store_shop_item_info($pos,$num,$count = -1)
   _DebugOut("get item info from: " & $num)

	Local $zx = $shop_ask[4] - $shop_box[0] - 29 * $shop_count + 58 * $num
	Local $zy = $shop_ask[5] - $shop_box[1] - 9
	
	Local $click_x, $click_y, $check_x, $check_y, $check_color, $l, $t, $r, $b
	
	$click_x = $zx + 6
	$click_y = $zy + 32
	$check_y = $click_y
	$t = - 190
	$b = 190 + 79
	If $num < 2 + Floor($shop_count / 2) Then
		$check_x = $click_x + 50
		$l = $click_x + 31
		$r = $l + 239 
	Else
		$check_x = $click_x - 50
		$r = $click_x - 32
		$l = $r - 239 
	EndIf
	$check_color = getPixel($shop_tBits, $shop_data_width, $check_x, $check_y)
	
	Local $rc = capture_shop_item_info($pos, _
		$click_x + $shop_box[0], $click_y + $shop_box[1], _
		$check_x + $shop_box[0], $check_y + $shop_box[1], $check_color, _
		$l + $shop_box[0], $t + $shop_box[1], _
		$r + $shop_box[0], $b + $shop_box[1])
	If $count >= 0 And $rc Then
		Local $file = $shop_path_info & "\item-" & $count & ".jpg"
		_ScreenCapture_SaveImage($file,$item_info_hBMP,True)
		$item_info_hBMP = 0
	EndIf
	Return $rc
EndFunc

Func capture_shop_item_info($pos, $click_x, $click_y, $check_x, $check_y, $check_color, $l, $t, $r, $b)
	If $item_info_durty Then
		If $item_info_hBMP Then
			_WinAPI_DeleteObject($item_info_hBMP)
		EndIf
		If $item_info_tBits Then
			_WinAPI_DeleteObject($item_info_tBits)
		EndIf
		$item_info_width = $r - $l + 1
		$item_info_height = $b - $t + 1
		$item_info_length = $item_info_width * $item_info_height
		$item_info_tBits = DllStructCreate('dword[' & $item_info_length & ']')
		$item_info_durty = True
		;_DebugOut("$item_info_width = " & $item_info_width)
		;_DebugOut("$item_info_height = " & $item_info_height)
		;_DebugOut("$item_info_length = " & $item_info_length)
	EndIf


	Local $p = $winlist[$pos]
	$check_x -= $l
	$check_y -= $t
	$l += $p[0] + $p[2]
	$r += $p[0] + $p[2]
	$t += $p[1] + $p[3]
	$b += $p[1] + $p[3]
	

	Local $try = 0
	While True
		Local $cur = MouseGetPos()
		MouseMove($click_x + $p[0] + $p[2], $click_y + $p[1] + $p[3],0)
		Sleep(500)
		$item_info_hBMP = _ScreenCapture_Capture("",$l,$t,$r,$b)
		If Not $item_info_hBMP Then
			_DebugOut("capture failed")
			Return False
		EndIf
		MouseMove($cur[0],$cur[1],0)
		Sleep(100)
	  
		_WinAPI_GetBitmapBits($item_info_hBMP, 4 * $item_info_length, DllStructGetPtr($item_info_tBits))
		Local $c = getPixel($item_info_tBits, $item_info_width, $check_x, $check_y)
		If $c = $check_color Then
			_WinAPI_DeleteObject($item_info_hBMP)
			$item_info_hBMP = 0
		Else
			ExitLoop
		EndIf
		$try += 1
		If $try > 20 Then ExitLoop
	WEnd
	
	If $item_info_hBMP Then
		Return True
	EndIf
	
	Return False
EndFunc

Func export_shop_reference()
	Local $fh = FileOpen($shop_file_out,BitOR(2,256));
	For $i = 0 To $shop_reference_cnt-1
		FileWrite($fh, $i & _
			" [0x" & Hex($shop_reference[$i][0],6) & _
			",0x" & Hex($shop_reference[$i][1],6) & _
			",0x" & Hex($shop_reference[$i][2],6) & _
			",0x" & Hex($shop_reference[$i][3],6) & _
			"]" & @CRLF)
	Next
	FileClose($fh)
EndFunc

Func html_shop_reference()
	Local $fh = FileOpen($shop_file_html,BitOR(2,256));
	FileWrite($fh, _
"<html><head>" & @CRLF & _
"<meta http-equiv='content-type' content='text/html; charset=utf-8'/>" & @CRLF & _
"<title>items</title>" & @CRLF & _
"<script src='jquery-1.8.2.min.js'></script>" & @CRLF & _
"<script src='all-items.js'></script>" & @CRLF & _
"<script>" & @CRLF & _
"var shop_path_info ='" & $shop_path_info & "';" & @CRLF & _
"</script>" & @CRLF & _
"<link rel='stylesheet' href='all-items.css'>" & @CRLF & _
"</head><body>" & @CRLF _
)
	FileWrite($fh, "<div id='out'><input id='iout' type='text'/></div>" & @CRLF)
	FileWrite($fh, "</div><div id='pre'><br></div><div id='mn'><table>" & @CRLF)
	FileWrite($fh, "<tr>" & @CRLF)
	For $i = 0 To $shop_reference_cnt-1
		FileWrite($fh, "<td class='si'><img src='../" & $shop_path_item & "/item-" & $i & ".jpg'></td>")
		If Mod($i+1,10) = 0 Then
			FileWrite($fh, "</tr><tr>" & @CRLF)
		EndIf
	Next
	FileWrite($fh, "</tr>" & @CRLF)
	FileWrite($fh, "</table></div>" & @CRLF)
	FileWrite($fh, "</body></html>" & @CRLF)
	FileClose($fh)
EndFunc

Func import_shop_reference()
	Local $fh = FileOpen($shop_file_in);
	$shop_reference_cnt = 0
	If $fh = -1 Then
		Return 0
	EndIf

	While True
		Local $line = FileReadLine($fh)
		If @error = -1 Then ExitLoop
		Local $ret = StringRegExp($line,"(\d+)\s+\[([^,]+),([^,]+),([^,]+),([^\]]+)\]",1)
		If @error = 0 Then
			For $j = 0 To 3
				$shop_reference[$ret[0]][$j] = $ret[$j+1]
			Next
			$shop_reference_cnt = $ret[0] + 1
		EndIf
	WEnd
	FileClose($fh)
	html_shop_reference()
EndFunc

Func get_item_level($pos,$place)
	If store_shop_item_info($pos,$place) Then
	;_DebugOut("call getItemLevel(" & $item_info_width & "," & $item_info_height & ")")
		Return getItemLevel($item_info_tBits,$item_info_width,$item_info_height)
	Else
		_DebugOut("cannot store_shop_item_info")
		Return 0
	EndIf
EndFunc
